package controller;

import business.Visualiser;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class PageController {

    @EJB
    Visualiser visualiser;

    //list of days and months to be display on comboboxes
    List<String> availableFromMonth;
    List<String> availableFromYear;
    List<String> availableToMonth;
    List<String> availableToYear;

    //image names and also their paths
    List<String> imagesCloud;
    List<String> imagesHisto;
    String imageCluster;

    String[] months = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};

    //the articles will be extracted from this 01/MM/YYYY
    String selectedFromMonth = "1";
    String selectedFromYear = "2014";
    //to this 01/MM/YYYY
    String selectedToMonth = "1";
    String selectedToYear = "2014";

    
    
    //==================================GETTERS AND SETTERS=====================================
    public Visualiser getVisualiser() {
        return visualiser;
    }

    public void setVisualiser(Visualiser visualiser) {
        this.visualiser = visualiser;
    }

    public String getSelectedFromMonth() {
        return selectedFromMonth;
    }

    public void setSelectedFromMonth(String selectedFromMonth) {
        this.selectedFromMonth = selectedFromMonth;
    }

    public String getSelectedFromYear() {
        return selectedFromYear;
    }

    public void setSelectedFromYear(String selectedFromYear) {
        this.selectedFromYear = selectedFromYear;
    }

    public String getSelectedToMonth() {
        return selectedToMonth;
    }

    public void setSelectedToMonth(String selectedToMonth) {
        this.selectedToMonth = selectedToMonth;
    }

    public String getSelectedToYear() {
        return selectedToYear;
    }

    public void setSelectedToYear(String selectedToYear) {
        this.selectedToYear = selectedToYear;
    }

    public List<String> getImagesHisto() {
        imagesHisto = new LinkedList<>();
        imagesHisto.add("h_lemonde");
        imagesHisto.add("h_figaro");
        imagesHisto.add("h_liberation");
        return imagesHisto;
    }

    public void setImagesHisto(List<String> imagesHisto) {
        this.imagesHisto = imagesHisto;
    }

    //getters and setters
    public List<String> getAvailableFromMonth() {
        availableFromMonth = new LinkedList<>();
        for (String s : months) {
            availableFromMonth.add(s);
        }
        return availableFromMonth;
    }

    public double getChiSquareTestValue() {
        return visualiser.getChiSquareTestValue();
    }

    public void setAvailableFromMonth(List<String> availableFromMonth) {
        this.availableFromMonth = availableFromMonth;
    }

    public List<String> getAvailableFromYear() {
        availableFromYear = new LinkedList<>();
        availableFromYear.add("2014");
        availableFromYear.add("2015");
        return availableFromYear;
    }

    public void setAvailableFromYear(List<String> availableFromYear) {
        this.availableFromYear = availableFromYear;
    }

    public List<String> getAvailableToMonth() {
        availableToMonth = new LinkedList<>();
        for (String s : months) {
            availableToMonth.add(s);
        }
        return availableToMonth;
    }

    public void setAvailableToMonth(List<String> availableToMonth) {
        this.availableToMonth = availableToMonth;
    }

    public List<String> getAvailableToYear() {
        availableToYear = new LinkedList<>();
        availableToYear.add("2014");
        availableToYear.add("2015");
        return availableToYear;
    }

    public void setAvailableToYear(List<String> availableToYear) {
        this.availableToYear = availableToYear;
    }

    public List<String> getImagesCloud() {
        imagesCloud = new LinkedList<>();
        imagesCloud.add("c_lemonde");
        imagesCloud.add("c_figaro");
        imagesCloud.add("c_liberation");
        return imagesCloud;
    }

    public void setImagesCloud(List<String> images) {
        this.imagesCloud = images;
    }
    
    //=================================================================================

    public String gotoSelectPeriod() {
        //check for available date
        availableFromMonth = new LinkedList<>();
        availableFromYear = new LinkedList<>();
        availableToMonth = new LinkedList<>();
        availableToYear = new LinkedList<>();
        return "period.xhtml?faces-redirect=true";
    }

    public String gotoCompare() {
        return "compare.xhtml?faces-redirect=true";
    }

    public double getChiSquareValue() {
        return visualiser.getChiSquareTestValue();
    }

    public String visualize() {
        imagesCloud = new LinkedList<>();
        imagesCloud.add("c_lemonde");
        imagesCloud.add("c_figaro");
        imagesCloud.add("c_liberation");

        imagesHisto = new LinkedList<>();
        imagesHisto.add("h_lemonde");
        imagesHisto.add("h_figaro");
        imagesHisto.add("h_liberation");
        imageCluster = "cluster";
        
        //calculate data and construct image
        SimpleDateFormat dayFormat = new SimpleDateFormat("dd/MM/yyyy");
        try {
            Date from = dayFormat.parse("01/" + selectedFromMonth + "/" + selectedFromYear);
            Date to = dayFormat.parse("01/" + selectedToMonth + "/" + selectedToYear);
            if (from.after(to))
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Date period not valid!"));
                return null;
            }
            else
            {   
                //create image method will generate the images and save them to disk
                visualiser.createImage(from, to, 30);
                return "visualization.xhtml?faces-redirect=true";
            }
            
        } catch (ParseException ex) {
            Logger.getLogger(PageController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "No data in that period!"));
        }
        return null;
    }
}
